<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>岗位分配</title>
<script type="text/javascript">
	var $tree;
	var $grid;
	var $gridStaff;
	var selectNode = "";//选中的树节点
	$(function() {

		tree = $("#tree").tree({
			method : "get",
			lines : true,
			url : "${ctx}/basic/postUsers/tree/",
			dataPlain : true, //该属性用以启用当前 easyui-tree 控件对平滑数据格式的支持
			onSelect : function(node) {
				if (node.text.indexOf("岗位") != -1) {
					selectNode = node;
					$("#cc").layout("panel", "east").panel({
						title : "【" + node.text + "】岗位分配"
					});
					$grid.datagrid({
						url : "${ctx}/basic/postUsers/dataGrid/" + node.id
					});
					$("#grid").datagrid('enableFilter');
				}
			}
		});
		$grid = $("#grid").datagrid($.extend(datagridOption, {
			url : "",
			columns : [ [ {
				field : 'id',
				checkbox : true
			}, {
				field : 'organization',
				title : '所属组织',
				sortable : true,
				width : 110,
				formatter : function(val) {
					return val == null ? " " : val.orgName;
				}
			}, {
				field : 'post',
				title : '主岗位',
				sortable : true,
				width : 110,
				formatter : function(val) {
					return val == null ? " " : val.postName;
				}
			}, {
				field : 'staffName',
				title : '员工姓名',
				sortable : true,
				width : 110,
			}, {
				field : 'identityCardNo',
				title : '身份证号码',
				width : 150,
				sortable : true
			}, {
				field : 'jobNumber',
				title : '工号',
				width : 110,
				sortable : true
			} ] ]
		}));
		$("#grid").datagrid('enableFilter', [ {
			field : 'organization',
			searchField : 'organization.orgName'
		}, {
			field : 'post',
			searchField : 'post.postName'
		} ]);
		$("#btn-add").click(function() {
			if (selectNode != "") {
				choose_staff();
			} else {
				$.messager.show({
					msg : "请先选择进行操作的岗位节点！",
					icon : "warning",
					position : "bottomRight"
				});
			}
		});
		$("#btn-delete").click(function(){
			var checkedRemoveUsers = $grid.datagrid("getChecked");
			if(checkedRemoveUsers == null && checkedRemoveUsers.length < 1){
				$.messager.show({
					msg : "请勾选需移除该岗位的用户！",
					icon : "warning",
					position : "bottomRight"
				});
				return false;
			}
			var ids = "";
			for(var i=0;i<checkedRemoveUsers.length;i++){
				if(checkedRemoveUsers[i].id){
					ids += checkedRemoveUsers[i].id + ",";
				}
			}
			$.post("${ctx}/basic/postUsers/removePost", {
				userIds : ids,
				postId : selectNode.id
			}, function(result) {
				$.messager.show({
					msg : result.msg,
					icon : "info",
					position : "bottomRight"
				});
				if (result.success) {
					$("#dlg1").dialog("close");
					$("#grid").datagrid("load").datagrid("clearSelections").datagrid("clearChecked");
					$("#grid").datagrid('enableFilter', [ {
						field : 'organization',
						searchField : 'organization.orgName'
					}, {
						field : 'post',
						searchField : 'post.postName'
					} ]);
				}
			}, 'json');
		});
	});
	function fun_postUsers_add() {
		var ids = "";
		var checkedUsers = $gridStaff.datagrid("getChecked");
		if (checkedUsers == null && checkedUsers.length < 1) {
			$.messager.show({
				msg : "请勾选被分配的用户！",
				icon : "warning",
				position : "bottomRight"
			});
			return false;
		}
		for (var i = 0; i < checkedUsers.length; i++) {
			if (checkedUsers[i].id) {
				ids += checkedUsers[i].id + ",";
			}
		}
		$.post("${ctx}/basic/postUsers/addPost", {
			userIds : ids,
			postId : selectNode.id
		}, function(result) {
			$.messager.show({
				msg : result.msg,
				icon : "info",
				position : "bottomRight"
			});
			if (result.success) {
				$gridStaff.datagrid("load").datagrid("clearSelections").datagrid("clearChecked");
				$("#dlg1").dialog("close");
				$("#grid").datagrid({
					url : "${ctx}/basic/post/showInfo/" + selectNode.id
				});
				$("#grid").datagrid('enableFilter', [ {
					field : 'organization',
					searchField : 'organization.orgName'
				}, {
					field : 'post',
					searchField : 'post.postName'
				} ]);
			}
		}, 'json');
	}
	function choose_staff() {
		$("#dlg1").dialog("open").dialog("setTitle", "选择人员");
		$gridStaff = $("#tb2").datagrid($.extend(datagridOption, {
			fitColumns : true,
			fit : false,
			toolbar : "#",
			url : "${ctx}/basic/postUsers/showInfo/" + selectNode.id,
			columns : [ [ {
				field : 'id',
				checkbox : true
			}, {
				field : 'organization',
				title : '所属组织',
				sortable : true,
				width : 110,
				formatter : function(val) {
					return val == null ? " " : val.orgName;
				}
			}, {
				field : 'post',
				title : '主岗位',
				sortable : true,
				width : 110,
				formatter : function(val) {
					return val == null ? " " : val.postName;
				}
			}, {
				field : 'staffName',
				title : '员工姓名',
				sortable : true,
				width : 110,
			} ] ]
		}));
		$("#tb2").datagrid('enableFilter', [ {
			field : 'organization',
			searchField : 'organization.orgName'
		}, {
			field : 'post',
			searchField : 'post.postName'
		} ]);
	}
</script>
</head>

<body>
	<div data-options="region:'center',fit:true,border:false">
		<div id="cc" class="easyui-layout" data-options="fit:true"
			style="width: 680px; height: 440px;">
			<div data-options="region:'center',title:'组织--岗位架构'"
				style="padding: 1px; witdh: 300px">
				<ul id="tree">
				</ul>
			</div>
			<div id="dlg"
				data-options="region:'east',split: true, collapsible:true,title : '岗位人员信息'"
				style="padding: 1px; witdh: 600px">
				<div id="tt" class="row">
				<div class="col-md-12">
					<shiro:hasPermission name="postUsers:update">
					<button id="btn-add" type="button" class="btn btn-primary" onclick="fun_user_add();">
					<span class="glyphicon glyphicon-plus"></span> 批量新增该岗位员工
				</button>
					</shiro:hasPermission>
					<shiro:hasPermission name="postUsers:delete">
					<button id="btn-delete" type="button" class="btn btn-danger">
					<span class="glyphicon glyphicon-minus"></span> 批量删除该岗位员工
				</button>
					</shiro:hasPermission>
					</div>
				</div>
				<table id="grid">
				</table>
			</div>
			<div id="dlg1" class="easyui-dialog"
				style="width: 600px; height: 430px; padding: 10px 20px" closed="true"
				data-options="resizable:true,modal:true, buttons:'#dlg-btn',closed:true">
				<form id="showInfofm" method="post">
					<input id="id" type="hidden" name="id" />
					<table id="tb2">
					</table>
					</form>
					<div id="dlg-btn">
				<button id="add" type="button" class="btn btn-primary" onclick="fun_postUsers_add();">
					<span class="glyphicon glyphicon-ok"></span> 保存
				</button>
				<button id="edit" type="button" class="btn btn-primary" onclick="$('#dlg').dialog('close');">
					<span class="glyphicon glyphicon-remove"></span> 取消
				</button>
			</div>
			</div>
		</div>
	</div>
</body>
</html>
